package com.dom.invoapp.modular.provider;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dom.api.sale.MarketSaleApi;
import com.dom.common.constants.Constants;
import com.dom.common.model.standard.ResponseModel;
import com.dom.common.model.vo.sale.*;
import com.dom.invoapp.dao.entity.TClientInfo;
import com.dom.invoapp.dao.entity.TmMarketSaleFlow;
import com.dom.invoapp.dao.service.ITClientInfoService;
import com.dom.invoapp.modular.service.MarketSaleService;
import com.xiaoleilu.hutool.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class MarketSaleProvider extends AbstractProvider implements MarketSaleApi {

    @Autowired
    private MarketSaleService marketSaleService;

    @Override
    public String insertMarketSale(@RequestBody M030R request) {
        super.isValid(request);
        return marketSaleService.insertMarketSale(request);
    }

    @Override
    public M039A getMarketSaleList(@RequestBody M039R request) {
        Page<TmMarketSaleFlow> page = marketSaleService.getMarketSaleList(request);
        M039A result = new M039A();
        result.setPs(page.getCurrent());
        result.setPn(page.getSize());
        result.setTotalNum((int)page.getTotal());
        result.setPages((int)page.getPages());
        Map<String,Object> map = marketSaleService.getAllClientInfoToMap();
        List<SaleInfo> list = new ArrayList<SaleInfo>();
        for (TmMarketSaleFlow flow: page.getRecords()) {
            SaleInfo info = builSale(flow,map);
            list.add(info);
        }
        result.setSaleInfoData(list);
        return result;
    }

    @Override
    public M033A findUpdateMarketSale(@RequestParam("id") String id) {
        return marketSaleService.findUpdateMarketSale(id);
    }

    @Override
    public boolean marketSaleDel(@RequestParam("id") String id) {
        return marketSaleService.marketSaleDel(id);
    }

    @Override
    public ResponseModel marketSaleModify(@RequestBody M032R request) {
        return marketSaleService.marketSaleModify(request);
    }

    private SaleInfo builSale(TmMarketSaleFlow flow,Map<String,Object> map) {
//        TODO 属性赋值
        SaleInfo info = new SaleInfo();
        info.setBillAbstract(flow.getDigest());
        info.setCancelStat(Integer.toString(flow.getCancelStat()));
        info.setCancelTime(flow.getCancelTime() == null ? null : DateUtil.format(flow.getCancelTime(), Constants.DATE_FORMAT_YYYY_MM_DD));
        info.setCompCode(flow.getOrganCode() == null ? null : flow.getOrganCode().toString());
        info.setCustCode(flow.getCustCode() == null ? null : flow.getCustCode().toString());
        info.setCustName(map.get(flow.getCustCode().toString()).toString());
        info.setSourBillType(flow.getSourBillType() == null ? null : flow.getSourBillType().toString());
        info.setSourBillId(flow.getSourBillId());
        info.setExchDate(flow.getExchDate() == null ? null : DateUtil.format(flow.getExchDate(),Constants.DATE_FORMAT_YYYY_MM_DD));
        info.setId(flow.getId().toString());
//        info.setLinkMan(flow.get);
//        info.setLinkManTel(flow.get);
//        info.setMakerName();
        info.setMaketDate(flow.getMakeDate() == null ? null : DateUtil.format(flow.getMakeDate(),Constants.DATE_FORMAT_YYYY_MM_DD));
//        info.setSaleDate(flow.get);
        info.setSaleType(flow.getSaleType());
        info.setStat(flow.getStat().toString());
        info.setVerfierId(flow.getVerfierId());
        info.setVerfyDate(flow.getVerfyDate() == null ? null : DateUtil.format(flow.getVerfyDate(),Constants.DATE_FORMAT_YYYY_MM_DD));
        info.setVerfyErrMsg(flow.getVerfyErrMsg());
//        info.setVerfyName(flow.getVerfierId());
        info.setVerfyStat(flow.getVerfyStat() == null ? null : flow.getVerfyStat().toString());
        return info;
    }

}
